var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server, {path: '/tienlen/socket.io'});
var config = require('./config');
var amqCon = require('../../common/util/AMQConnection');
var SocketController = require('./controller/SocketController');

var log4js = require('log4js');
var jslogger = log4js.getLogger('DaifugoWebServer');
log4js.configure('./tienlen/log4js.conf.json', {});

// create connection to rabbitmq server
var aCon = amqCon.getInstance();
aCon.connect(config.common.amq.host, config.common.amq.port, function (result) {
    if (result === true) {
        jslogger.debug('connected to rabbitmq server');
    } else {
        jslogger.debug('cannot connect to rabbitmq server');
        process.exit(1);
    }
});

// authentication
app.platformInfo = config.platformInfo;
app.everyauth = require("../common/controllers/authentication")(app, config.gameInfo);

// init configuration
require("./init")(app, express);

// register controllers
var sc = new SocketController(app);
sc.init(io);

var port = process.argv[2] || config.common.game_port;
jslogger.info('listen on: ' + port);

server.listen(port);

jslogger.info('server is listenning at: ' + port);
